package aos.ydksxt.service.common;

import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.dao.CsBdBddy;
import aos.ydksxt.dao.CsBdJgdy;
import aos.ydksxt.dao.CsBdTjdy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;

/**
 * 查询比对分析公共服务
 * Created by Stiofan on 2017/3/2.
 */
@Service
public class CxbdfxCommonService extends CommonService {
    @Override
    public void initView(HttpModel httpModel) {
        Dto params= Dtos.newDto("bddyUuid",httpModel.getInDto().getString("bddyUuid"));
        List<Map<String,Object>> tjdys=persistenceDAO.findAllBySql("select * from ydks.cs_bd_tjdy y where y.bddy_uuid=:bddyUuid order by y.xssx",params);
        httpModel.setAttribute("tjdys",tjdys);
        httpModel.setAttribute("bddyUuid",httpModel.getInDto().getString("bddyUuid"));
        httpModel.setViewPath("ydksxt/cxbdfx/cxbdfx.jsp");
    }
    @Transactional
    public void queryBdjgByBddyUuid(HttpModel httpModel){
        Dto inDto=httpModel.getInDto();
        //组装比对分析时所使用的参数
        Dto params=Dtos.newDto("bddyUuid",inDto.get("bddyUuid"));
        List<CsBdTjdy> tjdys=persistenceDAO.findAllByHql("from CsBdTjdy y where y.bddyUuid=:bddyUuid order by y.xssx",params);
        Dto tjdyMap=Dtos.newDto();
        for(CsBdTjdy tjdy:tjdys){
            tjdyMap.put(tjdy.getTjid(),inDto.get(tjdy.getTjid()));
        }
        List<CsBdBddy> bddys=persistenceDAO.findAllByHql("from CsBdBddy y where y.bddyUuid=:bddyUuid order by y.xssx",params);
        if(bddys.size()>3){
            httpModel.setOutMsg("后台存在多个比对分析内容，请检查（最多定义三个比对内容）!");
            return;
        }
        Map cxjgs=new HashMap();
        for (CsBdBddy bddy : bddys) {
            cxjgs.put(bddy.getSjly(),persistenceDAO.findAllBySql(bddy.getDyjb(), tjdyMap));
        }
        List<CsBdJgdy> jgdys=persistenceDAO.findAllByHql("from CsBdJgdy y where y.bddyUuid=:bddyUuid order by y.xssx",params);
        List<LinkedHashMap<String,Object>> bdfxjgs=new ArrayList<>();
        LinkedHashMap<String,Object> bdfxjg=new LinkedHashMap<>();
        bdfxjg.put("jgmc","相关数据结果集数量");
        List cxjgYdks=(List) cxjgs.get("以地控税");
        bdfxjg.put("jgz1",cxjgYdks!=null?cxjgYdks.size():"无查询结果");
        bdfxjg.put("jgz2",cxjgYdks!=null?cxjgYdks.size():"无查询结果");
        bdfxjg.put("jgz3",cxjgYdks!=null?cxjgYdks.size():"无查询结果");
        List cxjgHxzg=(List)cxjgs.get("核心征管");
        //bdfxjg.put("jgz2",cxjgHxzg!=null?cxjgHxzg.size():"无查询结果");
        List cxjgWbsj=(List)cxjgs.get("外部数据");
        //bdfxjg.put("jgz3",cxjgWbsj!=null?cxjgWbsj.size():"无查询结果");
        bdfxjgs.add(bdfxjg);
        if(true){
        //if(cxjgYdks!=null&&cxjgHxzg!=null&&cxjgWbsj!=null){
            //if(cxjgYdks.size()==1&&cxjgYdks.size()==cxjgHxzg.size()&&cxjgHxzg.size()==cxjgWbsj.size()){
            if(true){
                bdfxjg.put("bdjg","采集数据一致,可以进行后续比对!");
                for(CsBdJgdy jgdy:jgdys){
                    LinkedHashMap tmp=new LinkedHashMap();
                    tmp.put("jgmc",jgdy.getJgmc());
                    tmp.put("jgz1",((Map)cxjgYdks.get(0)).get(jgdy.getJgid()));
                    tmp.put("jgz2",((Map)cxjgYdks.get(0)).get(jgdy.getJgid()));
                    tmp.put("jgz3",((Map)cxjgYdks.get(0)).get(jgdy.getJgid()));
                    if("String".equals(jgdy.getJglx())){
                        if(((Map)cxjgYdks.get(0)).get(jgdy.getJgid()).equals(((Map)cxjgYdks.get(0)).get(jgdy.getJgid()))&&
                                ((Map)cxjgYdks.get(0)).get(jgdy.getJgid()).equals(((Map)cxjgYdks.get(0)).get(jgdy.getJgid()))){
                            tmp.put("bdjg","查询结果比对一致");
                        }else{
                            tmp.put("bdjg","查询结果比对不一致");
                        }
                    }
//                    tmp.put("jgz2",((Map)cxjgHxzg.get(0)).get(jgdy.getJgid()));
//                    tmp.put("jgz3",((Map)cxjgWbsj.get(0)).get(jgdy.getJgid()));
                    bdfxjgs.add(tmp);
                }
            }else{
                bdfxjg.put("bdjg","采集数据量不一致或存在多个需要比对的查询结果,无法进行后续比对!");
            }
        }else{
            bdfxjg.put("bdjg","采集数据量不一致或存在多个需要比对的查询结果,无法进行后续比对!");
        }
        for(int i=0;i<jgdys.size();i++){
            CsBdJgdy jgdy=jgdys.get(i);
            //Map bdfxjg=bdfxjgs.get(i);
            if(bdfxjg==null) bdfxjg = new LinkedHashMap();

        }
        httpModel.setOutMsg(AOSJson.toGridJson(bdfxjgs));
    }
    @Transactional
    public void saveBddy(HttpModel httpModel){
        Dto inDto=httpModel.getInDto();
        Dto params=Dtos.newDto("uuid",inDto.get("uuid"));
        CsBdBddy csBdBddy=(CsBdBddy)persistenceDAO.findSingleByHql("from CsBddy y where y.uuid=:uuid",params);
        if(csBdBddy==null){
            csBdBddy=new CsBdBddy();
        }
        AOSUtils.copyProperties(inDto,csBdBddy);
        if(StringUtils.isEmpty(csBdBddy.getUuid())){
            csBdBddy.setUuid(AOSUtils.getRandomUUID());
        }
        Dto outDto=Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(csBdBddy);
            outDto.setAppCode("suc");
        }catch (Exception e){
            outDto.setAppCode("err");
            logger.error(e);
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));

    }
}
